package edu.hawaii.webspider;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

import org.junit.Test;
/**
 * This class tests the webanalys static methods.
 * @author kenglish
 *
 */

public class TestWebAnalysisTool {
  private static final String goodUrl = "http://www.hackystat.org";
  private static final String invalidUrl = 
          "http://www.12323554657560--=-=';;'`.com/613/test/test.txt";
  private static final String textFileUrl = "http://www.kenglish77.com/613/tests/linktotext.html";  
  /**
   * Test IsValidHost() method. 
   *
   */
  @Test
  public void testIsValidHost() {
    String url = "http://www.google.com";
    assertTrue("Should return valid host",WebAnalysisTool.isValidHostUrl(url));
  }  
  /**
   * Test negative result of IsValidHost() method.
   */
  @Test
  public void testIsValidHostFail() {
    assertFalse("Should return invalid host",WebAnalysisTool.isValidHostUrl(invalidUrl));
  }
  /**
   * Test ProcessLink(), should add to the webresponse array.
   * @throws Exception from the web requests.
   */
  @Test
  public void testProcessLink() throws Exception {
    TotalLinks totalLinks = new TotalLinks();
    int oldSize = totalLinks.webResponses.size();
    WebRequest request = new GetMethodWebRequest(goodUrl);
    WebResponse response = totalLinks.webConversation.getResponse(request);
    
    totalLinks.processLink(response.getLinks()[0]);
    assertEquals("Should add to the Webresponses Hash map", 
                  totalLinks.webResponses.size(), oldSize + 1 );
  }
  /**
   * Test ProcessLink() failure, should not add to the webresponse array.
   * The test fixture (web page) only has one link, a link to a text file.
   * @throws Exception from the web requests. 
   */
  @Test
  public void testProcessLinkTextFile() throws Exception {
    TotalLinks totalLinks = new TotalLinks();
    WebRequest request = new GetMethodWebRequest(textFileUrl);
    WebResponse response = totalLinks.webConversation.getResponse(request);
    
    totalLinks.processLink(response.getLinks()[0]);
    
    assertEquals("Should add to the Webresponses Hash map", totalLinks.webResponses.size(), 0  );
  }  
  
}
